/**
 *
 */
package com.wateray.ipassbook.ui.model;

import java.util.Vector;

import javax.swing.tree.DefaultMutableTreeNode;

import com.wateray.ipassbook.commom.Constant;
import com.wateray.ipassbook.domain.Subject;
import com.wateray.ipassbook.ui.widget.treeTable.AbstractTreeTableModel;
import com.wateray.ipassbook.ui.widget.treeTable.TreeTableModel;
import com.wateray.ipassbook.util.LanguageLoader;

/**
 * @author c-bryu
 * 
 */
public class SubjectPageTableModel extends AbstractTreeTableModel implements
		TreeTableModel {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	protected Vector<String> columnNames;
	protected Vector<Object> dataVector;

	// Types of the columns.
	static protected Class[] cTypes = { TreeTableModel.class, String.class,
			String.class, String.class, String.class };

	public SubjectPageTableModel(Vector<String> columnNames,
			Vector<Object> dataVector) {
		this.columnNames = columnNames;
		this.dataVector = dataVector;
		DefaultMutableTreeNode top = new DefaultMutableTreeNode(LanguageLoader
				.getString("Subject"));
		createNodes(top);
		this.setRoot(top);

	}

	public Vector<Object> getData() {
		return dataVector;
	}

	public void setData(Vector<Object> entityVector) {
		dataVector = entityVector;
	}

	private void createNodes(DefaultMutableTreeNode top) {
		DefaultMutableTreeNode expense = new DefaultMutableTreeNode(LanguageLoader
				.getString("Expense"));
		DefaultMutableTreeNode income = new DefaultMutableTreeNode(LanguageLoader
				.getString("Income"));
		top.add(expense);
		top.add(income);
		
		DefaultMutableTreeNode subjectNode = null;
		for (int i = 0; i < dataVector.size(); i++) {
			Subject subject = (Subject) dataVector.elementAt(i);
			subjectNode = new DefaultMutableTreeNode(dataVector.elementAt(i));
			if(subject.getSubjectFlag().equals(Constant.EXPENSE_KBN)){
				expense.add(subjectNode);		
				for (int m = 0; m < subject.getChildrenList().size(); m++) {
					DefaultMutableTreeNode	childItem = new DefaultMutableTreeNode(subject
							.getChildrenList().get(m));
					subjectNode.add(childItem);
				}
			}
			else{
				income.add(subjectNode);
				for (int m = 0; m < subject.getChildrenList().size(); m++) {
					DefaultMutableTreeNode	childItem = new DefaultMutableTreeNode(subject
							.getChildrenList().get(m));
					subjectNode.add(childItem);
					System.out.println(subject);
				}
			}	
		}
	}

	/**
	 * @return The Object in the row.
	 * @param rowIndex
	 * 
	 * */
	public Object getObjectByNode(Object node) {
		Object obj = null;
		if (node instanceof DefaultMutableTreeNode) {
			obj = ((DefaultMutableTreeNode) node).getUserObject();
		} else {
			obj = node;
		}

		return obj;
	}

	// protected Vector getChildren(Object node){
	// SubjectEntity entity = null;
	//   
	// if(node instanceof DefaultMutableTreeNode){
	// Enumeration e = ((DefaultMutableTreeNode) node).children();
	// Vector v = new Vector();
	// while(e.hasMoreElements()){
	// v.add(e.nextElement());
	// }
	//             
	// return v;
	// }
	// if(getObjectByNode(node) instanceof SubjectEntity){
	// entity = (SubjectEntity)getObjectByNode(node);
	// return entity.getChildren();
	// }
	// else
	// {return null;}
	// }

	public Class<?> getColumnClass(int column) {
		return cTypes[column];
	}

	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnNames.size();
	}

	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return columnNames.get(column);
	}

	@Override
	public Object getValueAt(Object node, int column) {
		// TODO Auto-generated method stub
		Subject entity = null;
		if (getObjectByNode(node) == null) {
			return null;
		}
		
//		if(((DefaultMutableTreeNode) node).isLeaf()){
//			entity = (Subject) getObjectByNode(node);
//		}
		if(getObjectByNode(node) instanceof Subject){
			entity = (Subject) getObjectByNode(node);
		}
		else{
			return column == 0 ?getObjectByNode(node).toString() : "";
		}

		try {
			switch (column) {
			case 0:
				return entity.getName();
			case 1:
				return entity.getVisible();
			case 2:
				return entity.getOwner();
			case 3:
				return entity.getMemo();
			}
		} catch (SecurityException se) {
		}

		return null;
	}

	@Override
	public Object getChild(Object parent, int index) {
		// TODO Auto-generated method stub
		if (parent instanceof DefaultMutableTreeNode) {
			return ((DefaultMutableTreeNode) parent).getChildAt(index);
		} else {
			return null;
		}
	}

	@Override
	public int getChildCount(Object parent) {
		// TODO Auto-generated method stub
		int count = 0;
		if (parent instanceof DefaultMutableTreeNode) {
			count = ((DefaultMutableTreeNode) parent).getChildCount();
		}
		return count;
	}

//	 The superclass's implementation would work, but this is more efficient.
	 public boolean isLeaf(Object node) {
	    return ((DefaultMutableTreeNode) node).getChildCount() == 0;
	 }
}
